﻿using admin.cyt369.com.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace admin.cyt369.com.Controllers
{
    public partial class HomeController
    {
        [HttpGet]
        public virtual IActionResult GetMainMenus()
        {
            IEnumerable<Admin.CYT369.Com.DAL.Domains.Apps.SystemMenu> menus = null;
            var uid = this.GetUserId();
            if (Admin.CYT369.Com.DAL.Services.UserService.Instance.IsAdministrators(null, uid))
            {
                menus = Admin.CYT369.Com.DAL.Services.AppService.Instance.GetMenus(enable: true);
            }
            else
            {
                menus = Admin.CYT369.Com.DAL.Services.AppService.Instance.GetMenus(enable: true, userId: uid);
            }

            return DefaultJson(menus);
        }


        [HttpGet]
        public virtual IActionResult GetSubMenus(int id)
        {
            IEnumerable<Admin.CYT369.Com.DAL.Domains.Apps.SystemMenu> menus = null;
            var uid = this.GetUserId();
            if (Admin.CYT369.Com.DAL.Services.UserService.Instance.IsAdministrators(null, uid))
            {
                menus = Admin.CYT369.Com.DAL.Services.AppService.Instance.GetSubMenus(id, true);
            }
            else
            {
                menus = Admin.CYT369.Com.DAL.Services.AppService.Instance.GetSubMenus(id, true, userId: uid);
            }

            var m = menus.FirstOrDefault();
            if (m != null)
            {
                if (m.SubMenus.Count > 0)
                {
                    m.state = "open";
                }
            }

            return DefaultJson(menus);
        }

    }
}
